import java.util.ArrayList;
import java.util.List;

// 定义 N 叉树的节点结构
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
        children = new ArrayList<>();
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }

    // 添加子节点的方法
    public void addChild(Node child) {
        if (children == null) {
            children = new ArrayList<>();
        }
        children.add(child);
    }
}

public class Solution {
    public List<Integer> preorder(Node root) {
        List<Integer> res = new ArrayList<>();
        helper(root, res);
        return res;
    }

    public void helper(Node root, List<Integer> res) {
        if (root == null) {
            return;
        }
        res.add(root.val);
        if (root.children != null) {
            for (Node ch : root.children) {
                helper(ch, res);
            }
        }
    }

    public static void main(String[] args) {
        // 创建一个简单的 N 叉树进行测试
        // 构造如下树：
        //       1
        //    /  |  \
        //   3   2   4
        //  / \
        // 5   6
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        Node node3 = new Node(3);
        node3.addChild(node5);
        node3.addChild(node6);

        Node node2 = new Node(2);
        Node node4 = new Node(4);
        Node root = new Node(1);
        root.addChild(node3);
        root.addChild(node2);
        root.addChild(node4);

        Solution solution = new Solution();
        List<Integer> result = solution.preorder(root);
        System.out.println(result); // 输出应为 [1, 3, 5, 6, 2, 4]
    }
}